package net.loyintean.pearlshell.validation.rule;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.loyintean.pearlshell.validation.IValidRule;
import net.loyintean.pearlshell.validation.IValidRuleConfig;

class StringIdentifyCardValidRuleImpl extends AbstractStringValidRuleImpl
		implements IValidRule {
	StringIdentifyCardValidRuleImpl() {
		super();
	}

	// 2014-7-9 from林俊 to辰明 use capital letter start
	// DONE use capital letter for constants.
	private static final Pattern IDENTIFY_CARD = Pattern
			.compile("(\\d{15})|(\\d{17}[0-9xX])");

	// 2014-7-9 from林俊 to辰明 use capital letter end

	@Override
	public String errorMsg(IValidRuleConfig config) {

		// 2014-7-9 from林俊 to辰明 message error start
		// CLOSE this errorMsg must append to field name to compose a tip.
		// for example, "证件号码"+"请输入正确格式的身份证号". however, this tip is not so good.
		return "不符合身份证号的格式";
		// 2014-7-9 from林俊 to辰明 message error end
	}

	@Override
	protected boolean validString(String toValid, IValidRuleConfig config) {

		if (toValid.length() == 18 || toValid.length() == 15) {
			// 2014-7-9 from林俊 to辰明 format start
			// DONE use default format style and shortcut key of eclipse.
			// 2014-7-9 from林俊 to辰明 good job start
			// DONE use control flow to reduce new instance
			Matcher indetify_Card = IDENTIFY_CARD.matcher(toValid);
			return indetify_Card.find();
			// 2014-7-9 from林俊 to辰明 good job end
		}
		return false;
	}
}
